//
//  GQHRoleView.m
//  Xanthium
//
//  Created by GuanQinghao on 24/02/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHRoleView.h"
#import "GQHHeader.h"
#import "GQHCustomCell.h"

@interface GQHRoleView () <UITableViewDelegate,UITableViewDataSource> {
    
    NSArray *roleIconStringArray;
    NSArray *roleTitleArray;
    NSArray *roleDescriptionArray;
    
    NSArray *serviceIconStringArray;
    NSArray *serviceTitleArray;
}

@end

@implementation GQHRoleView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = kAppleGrayColor;
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
    roleIconStringArray = @[@"login_role_shipper",@"login_role_driver",@"login_role_enterprise"];
    roleTitleArray = @[@"我是货主",@"我是司机",@"我是物流企业"];
    roleDescriptionArray = @[@"可以在智运平台发布货源信息\n可以寻找货车信息",@"可以在智运平台发布空车信息\n可以寻找货源信息",@"可以在智运平台发布货源信息\n可以寻找企业内货车信息"];
    
    serviceIconStringArray = @[@"login_service_gas",@"login_service_etc",@"login_service_insurance",@"login_service_other"];
    serviceTitleArray = @[@"油卡充值",@"ETC",@"保险",@"途途行"];
}

#pragma mark --View
- (void)loadUserInterface {
    
    CGRect frame = CGRectMake(0, self.qh_statusBarHeight + self.qh_navigationBarHeight, self.qh_contentAreaWidth, self.qh_contentAreaHeight + self.qh_tabBarHeight + self.qh_homeIndicatorHeight);
    UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = kLightGrayColor;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:tableView];
}

#pragma mark --Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return roleTitleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 140.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    GQHRoleCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[GQHRoleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.qh_imageView.image = [UIImage imageNamed:roleIconStringArray[indexPath.row]];
    cell.qh_titleLabel.text = roleTitleArray[indexPath.row];
    cell.qh_descriptionLabel.text = roleDescriptionArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 选择角色
    if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {
        
        [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.25 * (self.qh_width - GQH_MARGIN_DOUBLE) + 30 + GQH_MARGIN_DOUBLE;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = kLightGrayColor;
    
    UIView *containerView = [[UIView alloc] init];
    containerView.layer.cornerRadius = 5.0;
    containerView.layer.masksToBounds = YES;
    containerView.backgroundColor = kWhiteColor;
    [footerView addSubview:containerView];
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.left.bottom.and.right.mas_equalTo(footerView).with.inset(GQH_MARGIN_SINGLE);
    }];
    
    CGFloat itemWidth = 0.25 * (self.qh_width - GQH_MARGIN_DOUBLE);
    CGFloat itemHeight = itemWidth + 30;
    
    for (NSUInteger i = 0; i < serviceIconStringArray.count; i++) {
        
        CGFloat left = i * itemWidth;
        
        UIButton *itemButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [itemButton setTag:(200 + i)];
        [itemButton setBackgroundColor:kWhiteColor];
        [itemButton addTarget:self action:@selector(clickedShortcutButton:) forControlEvents:UIControlEventTouchUpInside];
        [containerView addSubview:itemButton];
        [itemButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.size.mas_equalTo(CGSizeMake(itemWidth, itemHeight));
            make.top.mas_equalTo(0);
            make.left.mas_equalTo(containerView).with.offset(left);
        }];
        
        UIImageView *itemIconView = [[UIImageView alloc]init];
        [itemIconView setImage:[UIImage imageNamed:serviceIconStringArray[i]]];
        [itemButton addSubview:itemIconView];
        [itemIconView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.and.left.and.right.mas_equalTo(itemButton).with.inset(15);
            make.height.mas_equalTo(itemIconView.mas_width).with.multipliedBy(1.0);
        }];
        
        UILabel *itemTitleLabel = [[UILabel alloc]init];
        [itemTitleLabel setFont:kSmallerFont];
        [itemTitleLabel setText:serviceTitleArray[i]];
        [itemTitleLabel setTextColor:kDarkBlackColor];
        [itemTitleLabel setTextAlignment:NSTextAlignmentCenter];
        [itemButton addSubview:itemTitleLabel];
        [itemTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.height.mas_equalTo(30);
            make.left.and.right.and.bottom.mas_equalTo(itemButton);
        }];
    }
    
    return footerView;
}

- (IBAction)clickedShortcutButton:(UIButton *)sender {
    DLog(@"跳转服务页面");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_clickedShortcutButton:)]) {
        
        [_qh_delegate qh_clickedShortcutButton:sender];
    }
}

@end
